/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       viewed_list.h

	$Header: /game/viewed_list.h $

	$NoKeywords: $

 ************************************************************************/

#ifndef VIEWED_LIST_H_INCLUDED
#define VIEWED_LIST_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>
#include "list_viewer.h"

// -------------------------------------------------------
// list of items
// -------------------------------------------------------
template <class type>
class t_viewed_list : public std::vector<type>
{
public:
	t_viewed_list();
	virtual ~t_viewed_list();

	typedef std::vector<type> t_base;

	void attach( t_list_viewer* viewer );
	void detach( t_list_viewer* viewer );
	void push_back( type const& item );
	void erase( iterator index );
protected:
	typedef std::vector<t_list_viewer*> t_viewer_list;

	t_viewer_list m_viewers;
};

// -------------------------------------------------------
// implementation of list of items
// -------------------------------------------------------
template <class type>
inline t_viewed_list<type>::t_viewed_list()
{
}

template <class type>
t_viewed_list<type>::~t_viewed_list()
{
	t_viewer_list::iterator index;

	for (index = m_viewers.begin(); index != m_viewers.end(); index++)
		(*index)->detach();
}

template <class type>
inline void t_viewed_list<type>::attach( t_list_viewer* viewer )
{
	if (std::find( m_viewers.begin(), m_viewers.end(), viewer ) == m_viewers.end())
		m_viewers.push_back( viewer );
}

template <class type>
inline void t_viewed_list<type>::detach( t_list_viewer* viewer )
{
	t_viewer_list::iterator index;

	index = std::find( m_viewers.begin(), m_viewers.end(), viewer );
	if (index != m_viewers.end())
		m_viewers.erase( index );
}

template <class type>
inline void t_viewed_list<type>::push_back( type const& item )
{
	t_viewer_list::iterator index;

	t_base::push_back( item );
	for (index = m_viewers.begin(); index != m_viewers.end(); index++)
		(*index)->inserted( size() - 1 );
}

template <class type>
inline void t_viewed_list<type>::erase( iterator index )
{
	t_viewer_list::iterator viewer;
	int						i = index - begin();

	t_base::erase( index );
	for (viewer = m_viewers.begin(); viewer != m_viewers.end(); viewer++)
		(*viewer)->removed( i );
}

#endif // VIEWED_LIST_H_INCLUDED